package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.entity.SysOperLogDo;
import com.ruoyi.system.mapper.SysOperLogMapper;
import com.ruoyi.system.service.ISysOperLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 操作日志记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-27
 */
@RequiredArgsConstructor
@Service
public class SysOperLogServiceImpl implements ISysOperLogService {
    private final SysOperLogMapper sysOperLogMapper;

    /**
     * 查询操作日志记录
     */
    @Override
    public SysOperLogDo queryById(Long operId) {
        return sysOperLogMapper.selectById(operId);
    }

    /**
     * 查询操作日志记录列表
     */
    @Override
    public TableDataInfo<SysOperLogDo> queryPageList(SysOperLogDo entity, PageQuery pageQuery) {
        LambdaQueryWrapper<SysOperLogDo> lqw = buildQueryWrapper(entity);
        Page<SysOperLogDo> result = sysOperLogMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询操作日志记录列表
     */
    @Override
    public List<SysOperLogDo> queryList(SysOperLogDo entity) {
        LambdaQueryWrapper<SysOperLogDo> lqw = buildQueryWrapper(entity);
        return sysOperLogMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<SysOperLogDo> buildQueryWrapper(SysOperLogDo entity) {
        Map<String, Object> params = entity.getParams();
        LambdaQueryWrapper<SysOperLogDo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(entity.getTitle()), SysOperLogDo::getTitle, entity.getTitle());
        lqw.eq(entity.getBusinessType() != null, SysOperLogDo::getBusinessType, entity.getBusinessType());
        lqw.eq(StringUtils.isNotBlank(entity.getMethod()), SysOperLogDo::getMethod, entity.getMethod());
        lqw.eq(StringUtils.isNotBlank(entity.getRequestMethod()), SysOperLogDo::getRequestMethod, entity.getRequestMethod());
        lqw.eq(entity.getOperatorType() != null, SysOperLogDo::getOperatorType, entity.getOperatorType());
        lqw.like(StringUtils.isNotBlank(entity.getOperName()), SysOperLogDo::getOperName, entity.getOperName());
        lqw.like(StringUtils.isNotBlank(entity.getDeptName()), SysOperLogDo::getDeptName, entity.getDeptName());
        lqw.eq(StringUtils.isNotBlank(entity.getOperUrl()), SysOperLogDo::getOperUrl, entity.getOperUrl());
        lqw.eq(StringUtils.isNotBlank(entity.getOperIp()), SysOperLogDo::getOperIp, entity.getOperIp());
        lqw.eq(StringUtils.isNotBlank(entity.getOperLocation()), SysOperLogDo::getOperLocation, entity.getOperLocation());
        lqw.eq(StringUtils.isNotBlank(entity.getOperParam()), SysOperLogDo::getOperParam, entity.getOperParam());
        lqw.eq(StringUtils.isNotBlank(entity.getJsonResult()), SysOperLogDo::getJsonResult, entity.getJsonResult());
        lqw.eq(entity.getStatus() != null, SysOperLogDo::getStatus, entity.getStatus());
        lqw.eq(StringUtils.isNotBlank(entity.getErrorMsg()), SysOperLogDo::getErrorMsg, entity.getErrorMsg());
        lqw.eq(entity.getOperTime() != null, SysOperLogDo::getOperTime, entity.getOperTime());
        return lqw;
    }

    /**
     * 新增操作日志记录
     */
    @Override
    public Boolean insert(SysOperLogDo entity) {
        SysOperLogDo add = BeanUtil.toBean(entity, SysOperLogDo.class);
        validEntityBeforeSave(add);
        Boolean flag = sysOperLogMapper.insert(add) > 0;
        if (flag) {
            entity.setOperId(add.getOperId());
        }
        return flag;
    }

    /**
     * 修改操作日志记录
     */
    @Override
    public Boolean updateById(SysOperLogDo entity) {
        SysOperLogDo update = BeanUtil.toBean(entity, SysOperLogDo.class);
        validEntityBeforeSave(update);
        return sysOperLogMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysOperLogDo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除操作日志记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return sysOperLogMapper.deleteBatchIds(ids) > 0;
    }
}
